﻿// Open Source: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Project: YoProgramo.NET (http://code.google.com/p/yoprogramo/)
// Autor: Fabian Figueredo (info@fabianfigueredo.com.ar)
// Blog: blog.yoprogramo.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.ServiceLocation;
using YoProgramo.Domain.BusinessLogic.Service;
using YoProgramo.Domain.Core.Entities;
using YoProgramo.Domain.Core.Interfaces;

namespace YoProgramo.Domain.BusinessLogic.Extensions
{
    public static class CommentExtension {
        public static void MarkAsSpam(this IComment comment, IUser user, string ip, bool approved) {
            var spam = new MarkAsSpam<Comment>();
            spam.ByUser = user;
            spam.Approved = approved;
            spam.FromIPAddress = ip;
            spam.MarkedAt = DateTime.Now;
            spam.SpamFor = (Comment)comment;

            var service = ServiceLocator.Current.GetInstance<IMarkAsSpamService<Comment>>();
            service.Create(spam);
        }

        public static void Vote(this IComment comment, IUser user, string ip, int rate) {
            var service = ServiceLocator.Current.GetInstance<IVoteService<Comment>>();
            service.VoteFor((Comment) comment, rate, user, ip);
        }

        public static bool CanVote(this IComment comment, IUser user) {
            var service = ServiceLocator.Current.GetInstance<IVoteService<Comment>>();
            return !service.HasVoted((Comment)comment, user);
        }

        public static int GetScore(this IComment comment) {
            var service = ServiceLocator.Current.GetInstance<IVoteService<Comment>>();
            return service.GetScore((Comment)comment);
        }

        public static bool IsSpam(this IComment comment) {
            var service = ServiceLocator.Current.GetInstance<IMarkAsSpamService<Comment>>();

            foreach(IMarkAsSpam<Comment> s in service.GetAllHasSpammerAproved())
            {
                if ((IComment)s.SpamFor == comment)
                    return true;
            }
            return false;            
        }

    }
}